﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Channels;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static LiWi.Shell;

namespace LiWi
{
    public partial class Io控制 : Form
    {
        public WindowsAdapt adapt = new WindowsAdapt();
        IntPtr _handle { get { return Data.handle; } }
        bool isConnect { get { return Data.handle == (IntPtr)0 ? false : true; } }




        uint[] InAll=new uint[100];              //输入口数量
        uint[] OutAll=new uint[50];             //输出口数量

        public Io控制()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            for (int i = 0; i < 50; i++)
            {
                OutAll[i] = 0;
            }
            for (int i = 0; i < 100; i++)
            {
                InAll[i] = 0;
            }
            InitLight();
            SetOutState();

            adapt.SetForm(this);
        }




        /// <summary>
        /// 初始化控件
        /// </summary>
        public void InitLight()
        {
            for (int i = 0; i < 100; i++)
            {
                var In_Num = "labLightIn" + i;
                var INNum = this.Controls.Find(In_Num, true).FirstOrDefault() as Label;
                INNum.BackColor = Color.LightGray;
            }
            for (int i = 0;i<50;i++)
            { 

                var Out_Num = "btnOut" + i;
                var OUTNum = this.Controls.Find(Out_Num, true).FirstOrDefault() as Button;
                OUTNum.BackColor = Color.White;
            }

        }

        /// <summary>
        /// 初次读取输出状态
        /// </summary>
        public void InitGetOut()
        {
            if (isConnect)
            {

                for (int i = 0; i < 50; i++)
                {
                    uint a = 2;
                    PMC_GetOp(_handle, i, ref a);
                    if (OutAll[i] != a)
                    {
                        if (a == 1 )
                        {
                           var Out_Num = "btnOut" + i;
                           var OUTNum = this.Controls.Find(Out_Num, true).FirstOrDefault() as Button;
                           OUTNum.BackColor = Color.LightGray;
                           //OutAll[i] = 1;
                        }
                        else
                        {
                            var Out_Num = "btnOut" + i;
                            var OUTNum = this.Controls.Find(Out_Num, true).FirstOrDefault() as Button;
                            OUTNum.BackColor = Color.White;
                            //OutAll[i] = 0;
                        }
                        OutAll[i] = a;
                    }
                
                }

            }
        }

        /// <summary>
        /// 设置输出
        /// </summary>
        public void SetOutState()
        {

                for (int i = 0; i <50; i++)
                {
                    int outthis = i;
                    var Out_Num = "btnOut" + i;
                    var OUTNum = this.Controls.Find(Out_Num, true).FirstOrDefault() as Button;
                    OUTNum.Click += (sender, e) =>
                    {
                        if (Data.handle != (IntPtr)0)
                        {
                            if (OutAll[outthis] == 1)
                            {
                                PMC_SetOp(_handle, outthis, 0);
                                OUTNum.BackColor = Color.White;
                                OutAll[outthis] = 0;
                            }
                            else if (OutAll[outthis] == 0)
                            {
                                PMC_SetOp(_handle, outthis, 1);
                                OUTNum.BackColor = Color.LightGray;
                                OutAll[outthis] = 1;
                            }
                        }
                        else MessageBox.Show("请先连接控制器");
                    };
                }


        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (isConnect)
            {
                for (int i = 0; i < 100; i++)
                {
                    uint value = 2;
                    PMC_GetIn(_handle, i, ref value);
                    if (InAll[i] != value)
                    {
                        if (value == 0)
                        {
                            var In_Num = "labLightIn" + i;
                            var INNum = this.Controls.Find(In_Num, true).FirstOrDefault() as Label;
                            INNum.BackColor = Color.LightGray;
                            //InAll[i] = 0;
                        }
                        else
                        {
                            var In_Num = "labLightIn" + i;
                            var INNum = this.Controls.Find(In_Num, true).FirstOrDefault() as Label;
                            INNum.BackColor = Color.Green;
                            //InAll[i] = 1;
                        }
                        InAll[i] = value;
                    }

                }
                InitGetOut();
            }
        }
    }
}
